package com.spring.sunflower.common

import android.content.pm.ActivityInfo
import android.view.WindowManager
import com.google.gson.Gson
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseCompatActivity
import com.spring.sunflower.bean.BeautyConfigBean
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.conversation.beauty.utils.BeautyUtils
import com.spring.sunflower.conversation.beauty.view.BeautyPanel
import com.spring.sunflower.dialog.OnPermissionIntroClickListener
import com.tencent.rtmp.ui.TXCloudVideoView
import com.tencent.trtc.TRTCCloud

/**
 * Created by Kevin on 2021/8/30<br/>
 *
 * Describe:<br/>
 */
class BeautyPreviewActivity : BaseCompatActivity() {
    private var txLocalVideoView: TXCloudVideoView? = null
    private var mTRTCCloud: TRTCCloud? = null
    private var beautyPanel: BeautyPanel? = null
    private var filterIndex = 1
    override fun doSomethingBeforeOnCreate() {
        super.doSomethingBeforeOnCreate()
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    override fun getLayoutID(): Int = R.layout.activity_beauty_preview
    override fun initView() {
//        mGrantedCameraPermission = true
        txLocalVideoView = findViewById(R.id.txLocalVideoView)
        txLocalVideoView?.setOnClickListener {
            beautyPanel?.let {
                if (!it.isShowing) {
                    it.show()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val granted = XXPermissions.isGranted(this, Permission.CAMERA)
        if (!granted) {
            showPermissionIntroDialog(
                arrayOf(
//                    getString(R.string.permission_audio_title),
                    getString(R.string.permission_camera_title)
                ),
                arrayOf(
//                    getString(R.string.permission_audio_desc),
//                    getString(R.string.permission_camera_desc)
                    "您当前正在进行美颜预览，该功能需要打开手机摄像头才能使用，如拒绝该权限，将退出页面"
                ),
                object : OnPermissionIntroClickListener {
                    override fun onClickOK() {
                        XXPermissions.with(this@BeautyPreviewActivity)
                            .permission(Permission.CAMERA)
                            .request(object : OnPermissionCallback {
                                override fun onGranted(permissions: List<String>, all: Boolean) {
                                    if (all) {
                                        initV()
                                    } else {
                                        showToast("无法美颜预览")
                                        finish()
                                    }
                                }

                                override fun onDenied(permissions: List<String>, never: Boolean) {
                                    showToast("无法美颜预览")
                                    finish()
                                }
                            })
                    }

                    override fun onClickCancel() {
                        showToast("已退出美颜预览")
                        finish()
                    }
                })
        } else {
            initV()
        }
    }

    private fun initV() {
        mTRTCCloud = TRTCCloud.sharedInstance(this)
        mTRTCCloud!!.startLocalPreview(true, txLocalVideoView)
        initBeauty()
    }

    private fun initBeauty() {
        var beautyJson = getField(Constant.KEY_BEAUTY_CONFIG, "")
        if (beautyJson.isNullOrEmpty()) {
            beautyJson = BeautyUtils.readAssetsFile("default_beauty_data.json")
        } else {
            val bean = Gson().fromJson(
                beautyJson,
                BeautyConfigBean::class.java
            )
            filterIndex = bean.beautyTabList[1].currentSelectFilterIndex
        }
        Constant.tempBeautyConfig = beautyJson
        saveField(Constant.KEY_BEAUTY_CONFIG, beautyJson)
        val beautyInfo = BeautyUtils.createBeautyInfo(beautyJson)
        if (beautyPanel == null) {
            beautyPanel = BeautyPanel(this)
            beautyPanel?.let {
                it.setBeautyInfo(beautyInfo)
                it.setBeautyManager(mTRTCCloud!!.beautyManager)
                it.setCurrentFilterIndex(filterIndex)
                it.setCurrentBeautyIndex(0)
                if (!it.isShowing) {
                    it.show()
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()
        mTRTCCloud = null
        TRTCCloud.destroySharedInstance()
        beautyPanel = null
    }

    override fun onStop() {
        super.onStop()
        printD("BeautyPreview onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        printD("BeautyPreview onDestroy")
    }


}